package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.QueryPageBean;
import com.itheima.exception.BusinessRuntimeException;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

	@Autowired
	UserDao userDao;
    @Autowired
    RoleDao roleDao;


	@Override
	public boolean login(String username, String password) {
		log.debug("service_provide...u:"+username+" p:"+password);
		if("admin".equals(username) && "123".equals(password)){
			return true;
		}else{
			throw new BusinessRuntimeException("登录失败!!!");
		}
	}

	@Override
	public SysUser findByUsername(String username) {

        SysUser byUsername = userDao.findByUsername(username);
        return byUsername;
    }

	/**
	 * 添加用户信息
	 * @param sysUser
	 */
	@Override
	@Transactional
	public int addUser(SysUser sysUser) {
		//添加用户信息
		userDao.addUser(sysUser);
		log.debug("========================================"+sysUser.getId());
		return sysUser.getId();
	}

	/**
	 * 添加用户角色信息
	 * @param userId
	 * @param roleId
	 */
	@Override
	@Transactional
	public void addUserRole(Integer userId, Integer roleId) {
		userDao.addUserRole(userId,roleId);
	}



    public Map<String, Object> findUserByPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<SysUser> users = userDao.findByPage(queryPageBean.getQueryString());
        List<Map<String, Object>> userList = new ArrayList<>();
//		List<SysUser> users1 = new ArrayList<>();

        for (SysUser user : users) {
            StringBuffer sbPermission = new StringBuffer();
            StringBuffer sbRoles = new StringBuffer();
            for (Role role : user.getRoles()) {
                for (Permission permission : role.getPermissions()) {
                    sbPermission.append(" " + permission.getName());
                }
            }
            for (Role role : user.getRoles()) {
                sbRoles.append(role.getName());
            }
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("user", user);
            map.put("roles", sbRoles.toString());
            map.put("permission", sbPermission.toString());
            userList.add(map);
        }
//		return new PageResult(users.getTotal(),users);
        HashMap<String, Object> mapResult = new HashMap<>();
        mapResult.put("total", users.getTotal());
        mapResult.put("users", userList);
        System.out.println(mapResult.toString());
        return mapResult;
    }

    @Override
    @Transactional
    public void delUserById(Integer id) {
        Integer count = userDao.findUser_RoleByUserId(id);
        if (count > 0)
            userDao.delUser_RoleByUserId(id);
        userDao.delUserByUserId(id);
    }

    @Override
    public SysUser findUserById(Integer id) {
        SysUser user = userDao.findUserById(id);
        return user;
    }

    @Override
    @Transactional
    public void updateUser(SysUser sysUser,Integer[] rolesId) {
        Integer count = userDao.findUser_RoleByUserId(sysUser.getId());
        if (count > 0)
            userDao.delUser_RoleByUserId(sysUser.getId());
        userDao.updateUser(sysUser);
        for (Integer roleId : rolesId) {
            userDao.addUserRole(sysUser.getId(),roleId);
        }
    }

    @Override
    public List<Role> findAllRoleY() {
        List<Role> roles = roleDao.findAllRoleY();
        return roles;
    }

}